using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WuLibrary;


namespace  TowerDefence
{
    public class Tower : MonoBehaviour
    {
        private float mAttackRang = 3f;

        private Enemy mAttackTarget;

        private int mAttack;

        private float mAttackCD = 0.8f;

        private float mCDTime = 0f;

        public string BulletModelName;

        public float BulletMoveSpeed;
        
        private Animation mAnimation;

        private string mAttackAnimName;

        private void Start()
        {
            mAnimation=GetComponent<Animation>();
            mAnimation["Idle"].layer = -1;
        }


        private void Update()
        {
            mCDTime+=Time.deltaTime;
            if (mCDTime >= mAttackCD)
            {
                mCDTime = 0f;
                
                if (mAttackTarget == null)
                {
                    mAttackTarget = GetAttackTarget();
                    if (mAttackTarget != null)
                    {
                        transform.LookAt(mAttackTarget.transform);
                        Attack(mAttackTarget);
                        
                    }
                }
                else if(IsAttackEnemy(mAttackTarget))
                {
                    
                   Attack(mAttackTarget);
                }
                else
                {
                    mAttackTarget = GetAttackTarget();
                 
                    if (mAttackTarget != null)
                    {
                       
                        transform.LookAt(mAttackTarget.transform);
                        Attack(mAttackTarget);
                    }
                }
                
                
            }
           
        }

        public void InitData(TowerInfo info)
        {
            mAttack = info.Attack;
            mAttackCD = info.AttackCD;
            mAttackRang = info.AttackRank;
            BulletModelName=info.BulletModelName;
            BulletMoveSpeed=info.BulletMoveSpeed;
            mAttackAnimName = info.AttackAnimName;
        }
        private Enemy GetAttackTarget()
        {
            for (int i = 0; i < BattleManager.mEnemyList.Count; i++)
            {
                if (Vector3.Distance(transform.localPosition, BattleManager.mEnemyList[i].transform.localPosition) <=
                    mAttackRang)
                {
                    return BattleManager.mEnemyList[i];
                }
            }

            return null;
        }
        private bool IsAttackEnemy(Enemy enemy)
        {
            if (enemy == null) return false;
            
            if (Vector3.Distance(transform.localPosition, enemy.transform.localPosition) <=
                mAttackRang)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void Attack(Enemy enemy)
        {
          AnimationState animSate=  mAnimation.PlayQueued(mAttackAnimName, QueueMode.PlayNow,PlayMode.StopSameLayer);
          if (mAttackCD < 1)
          {
              animSate.speed = 1 / mAttackCD;
          }
          else
          {
              animSate.speed = 1;
          }

          StartCoroutine(AttackBullet(enemy, enemy.transform.localPosition,animSate.speed * 0.5f));
        }

        private IEnumerator AttackBullet(Enemy enemy,Vector3 pos,float time)
        {
            yield return new WaitForSeconds(time);
            AudioManager.PlaySound("attack");
                Bullet bullet = BulletManager.CreatButtle(BulletModelName);
                if (enemy == null)
                {
                    bullet.InitData(pos,BulletMoveSpeed,mAttack);
                }
                else
                {
                    bullet.InitData(enemy,5,5);
                }
                bullet.transform.localPosition=transform.localPosition;
            
        }
       
        
    }
}

